package com.example.liang.basamovie.activity;

import android.app.Instrumentation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liang.basamovie.R;
import com.example.liang.basamovie.adapter.SeleteFilmAdapter;
import com.example.liang.basamovie.bean.SeleteItemBean;
import com.example.liang.basamovie.json.FindSeleteJson;
import com.example.liang.basamovie.utils.DataPath;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

/**
 * 搜索电影的Activity
 */
public class SeleteFilmActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    ImageView Selete_Back,Selete_clear;
    EditText Selete_EditText;
    TextView SeleteButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selete_film);
        recyclerView= (RecyclerView) findViewById(R.id.Selete_RecyclerView);
        Selete_Back= (ImageView) findViewById(R.id.Selete_Back);
        Selete_clear= (ImageView) findViewById(R.id.Selete_clear);
        Selete_EditText= (EditText) findViewById(R.id.Selete_EditText);
        SeleteButton= (TextView) findViewById(R.id.SeleteButton);
        initView();
        //点击事件的方法
        MyOnClickListener();
    }

    private void MyOnClickListener() {
        //点击图标返回的点击事件
        Selete_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            //Log.e("Exception when sendKeyDownUpSync"e.toString());
                        }
                    }
                }.start();
            }
        });
        //点击清除内容图标的点击事件
        Selete_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Selete_EditText.setText("");
                Selete_clear.setVisibility(View.GONE);

            }
        });
        //输入框文字变化的点击事件
        Selete_EditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String istext = Selete_EditText.getText().toString();
                if(TextUtils.isEmpty(istext)){
                    Selete_clear.setVisibility(View.GONE);
                }else {
                    Selete_clear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //点击搜索的点击事件
        SeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Toast.makeText(SeleteFilmActivity.this,"搜索点击事件",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //获取数据
        getData();
    }

    private void getData() {
        OkHttpUtils.get().url(DataPath.SELETEFILM_PATH).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(SeleteFilmActivity.this,"SeleteFilmActivity下载出错",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                List<SeleteItemBean> list = FindSeleteJson.getList(response);
                SeleteFilmAdapter adapter = new SeleteFilmAdapter(list, SeleteFilmActivity.this);
                recyclerView.setAdapter(adapter);

            }
        });
    }

}
